package com.zhuiyun.pm.client.login.utils;/**
 * @author syc
 * @date 2023/3/21 19:07
 */

import com.zhuiyun.pm.utils.MailUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @ClassName EmailUtils
 * @Description TODO
 * @Author sgo
 * @Date 2023/3/21 19:07
 **/
@Configuration
public class EmailUtils {
    @Resource
    private  JavaMailSender javaMailSender;
    @Value("${spring.mail.from}")
    private String fromEmail;


    /**
     * @param text 发送邮件内容
     * @param subject 主题
     * @param to 发给谁
     * @param from 发送者
     */
    public void sendEmail(String text,String subject,String to,String from){
        SimpleMailMessage message=new SimpleMailMessage();
        if(!StringUtils.isEmpty(from)){
            fromEmail = from;
        }
        message.setTo(to);//发给谁
        message.setFrom(fromEmail);//发送者
        message.setText(text);//文章内容
        message.setSubject(subject); //主题

            javaMailSender.send(message);


    }


    /**
     * 发送带有样式的内容
     * @param context 发送邮件内容 包含html
     * @param subject 主题
     * @param to 发给谁
     * @param from 发送者
     */
    public void sendEmailAndHtml(String context,String subject,String to,String from) throws MessagingException {
        // String context = "<p style=\"background-color: red;color: white\">我是谁</p>";
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        if(!StringUtils.isEmpty(from)){
            fromEmail = from;
        }
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setFrom(fromEmail);
        mimeMessageHelper.setText(context,true);
        mimeMessageHelper.setSubject(subject);
        javaMailSender.send(mimeMessage);
    }


    /**
     * 发送带有文件的邮件
     * @param text 发送邮件内容
     * @param subject 主题
     * @param to 发给谁
     * @param from 发送者
     * @throws MessagingException
     */
    public void sendEmailAndFile(String text,String subject,String to,String from,String filePath) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setFrom(fromEmail);
        mimeMessageHelper.setText(text);
        mimeMessageHelper.setSubject(subject);
        FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
        String filename = fileSystemResource.getFilename();
        mimeMessageHelper.addAttachment(filename, fileSystemResource);
        javaMailSender.send(mimeMessage);
    }


}
